#include <iostream>
#include <math.h>
#include <string.h>
using std::cout;
using std::endl;

class singleton{
public:
    static singleton * getsingleton(int num,const char* str){
        if(p_sin == nullptr){
            p_sin = new singleton(num,str);
        }
        return p_sin;
    }
    void print(){
        cout << "num:"<<_num<<" str:"<<_str<<endl;
    }
    void destroy(){
        delete p_sin;
        p_sin = nullptr;
        cout << "singleton delete" << endl;
    }

private:
    singleton(int num,const char* str)
    :_num(num)
    ,_str(new char[strlen(str)+1]())
    {
        strcpy(_str,str);
        cout << "singleton(int num, const char* str)" <<endl;
    }
    //删除拷贝构造函数和赋值运算函数
    ~singleton(){
        if(_str){
            delete [] _str;
            _str = nullptr;
        }
        cout << "~singleton" << endl;
    }
    singleton & operator=(singleton & rhs) = delete;
    static singleton * p_sin;
    int _num;
    char* _str;
};
singleton* singleton::p_sin = nullptr;


void test(){
    singleton* p1 = singleton::getsingleton(10,"hello"); 
    p1->print();
    singleton* p2 = singleton::getsingleton(110,"world");
    p2->print();
    p2->destroy();
    singleton* p3 = singleton::getsingleton(110,"world");
    p3->print();
    p3->destroy();
}

int main()
{
    

    test ();
    return 0;
}

